package com.mt.fpb.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mt.fpb.enums.ResponseCode;
import com.mt.fpb.mapper.*;
import com.mt.fpb.model.InsurancePolicy;
import com.mt.fpb.model.RegisterUser;
import com.mt.fpb.model.RxEmployee;
import com.mt.fpb.model.vo.CommonResult;
import com.mt.fpb.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;

import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
 * FileName: InsurancePolicyController
 * Author:   wang
 * Date:     2021/7/24 14:59
 * 保单接口
 */

@RestController
@Slf4j
@RequestMapping("/policy")
public class InsurancePolicyController {
    @Resource
    private InsurancePolicyMapper insurancePolicyMapper;



    @Autowired
    private RegisterMapper registerMapper;

    @Autowired
    private RxEmployeeMapper rxEmployeeMapper;
    /**
     * 获得保单信息
     */
    @GetMapping("/getpolicy")
    public CommonResult getInsurancePolicyById(String openId) {
        //根据openId获取到注册表中的名字、手机号、身份证号
        RxEmployee r = rxEmployeeMapper.getInfoByOpenId(openId);
        if (BeanUtil.isEmpty(r.getName()) || BeanUtil.isEmpty(r.getIdCard()) ||  BeanUtil.isEmpty(r.getPhoneNumber())) {
            return CommonResult.fail(500, "查询条件不足");
        }

        //根据接收到的保单查询数据库
        List<InsurancePolicy> insurance = insurancePolicyMapper.select(r.getIdCard(), r.getName(), r.getPhoneNumber());
        Date date = new Date();
        JSONArray jsonArray =new JSONArray();
        for (InsurancePolicy insurancePolicy : insurance) {
           // if (date.after(insurancePolicy.getEndTime())) {
                insurancePolicyMapper.updateState(insurancePolicy.getCertificateNumberRecognizee());
               // insurancePolicy.setState(0);
            //}
            jsonArray.add(insurancePolicy);
        }
       return CommonResult.success(jsonArray);
    }


}
